package Exams.MeituanPack.q2021.q02_store;

import java.util.Scanner;

class UF {
    int[] parent;
    int[] size;

    public UF(int n, int[] weight) {
        parent = new int[n];
        size = new int[n];
        for (int i = 0; i < n; i++) {
            parent[i] = i;
            size[i] = weight[i];
        }
    }

    public int find(int x) {
        while (parent[x] != x) {
            parent[x] = parent[parent[x]];
            x = parent[x];
            ;
        }
        return x;
    }

    public boolean connected(int x, int y) {
        int root_x = find(x);
        int root_y = find(y);
        return root_x == root_y;
    }

    public void union(int x, int y) {
        int root_x = find(x);
        int root_y = find(y);
        if (root_y == root_x) {
            return;
        }
        if (size[root_x] >= size[root_y]) {
            parent[root_y] = root_x;
            size[root_x] += size[root_y];
        } else {
            parent[root_x] = root_y;
            size[root_y] += size[root_x];
        }
    }

    public int getSize(int x) {
        int root_x = find(x);
        return size[root_x];
    }
}

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = Integer.parseInt(sc.nextLine());
        int[] nums = new int[n];
        for (int i = 0; i < n; i++) {
            nums[i] = sc.nextInt();
        }
        int[] order = new int[n];
        for (int i = 0; i < n; i++) {
            order[i] = sc.nextInt();
        }
        // System.out.println(Arrays.toString(nums));
        // System.out.println(Arrays.toString(order));
        UF uf = new UF(n, nums);
        int[] ans = new int[n + 1];
        ans[n] = 0;
        boolean[] visited = new boolean[n];
        for (int i = n - 1; i >= 1; i--) {
            int pos = order[i] - 1;
            // System.out.println(pos);
            visited[pos] = true;
            if (pos - 1 >= 0 && visited[pos - 1]) {
                uf.union(pos, pos - 1);
            }
            if (pos + 1 < n && visited[pos + 1]) {
                uf.union(pos, pos + 1);
            }
            ans[i] = Math.max(ans[i + 1], uf.getSize(pos));
        }
        for (int i = 1; i <= n; i++) {
            System.out.println(ans[i]);
        }

    }
}

